package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.CustomersDto;
import com.qk.entity.Customer;
import com.qk.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/customers")
public class CustomerController {
    @Autowired
    private CustomerService customerService;


    /**
     * 获取客户列表
     * @param customersDto
     * @return
     */
    @GetMapping
    public Result list(CustomersDto customersDto) {
        log.info("查询列表: {}", customersDto);
        PageResult<Customer> pageResult = customerService.listCustomers(customersDto);
        return Result.success(pageResult);
    }

    /**
     * 添加客户
     */
    @PostMapping
    public Result add(@RequestBody Customer customer) {
        log.info("添加客户: {}", customer);
        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());
        customerService.save(customer);
        return Result.success();
    }

    /**
     * 根据id查询客户
     */
    @GetMapping("/{id}")
    public Result getCustomerById(@PathVariable Integer id) {
        log.info("查询客户id: {}", id);
        Customer customer = customerService.getById(id);
        return Result.success(customer);
    }

    /**
     * 修改客户
     */
    @PutMapping
    public Result update(@RequestBody Customer customer) {
        log.info("修改: {}", customer);
        customer.setUpdateTime(LocalDateTime.now());
        customerService.updateById(customer);
        return Result.success();
    }
}
